<?php
include 'config.php';
$GET_userID = $_GET['u_id'];
$GET_title = $_GET['title'];
$GET_start_date = $_GET['start_date'];
$GET_start_time = $_GET['start_time'];
$GET_end_time = $_GET['end_time'];
$GET_weekday = $_GET['weekday'];
$GET_repeats = $_GET['repeats'];
$GET_repeats_freq = $_GET['repeats_freq'];
$GET_location  = $_GET['location'];
$GET_description = $_GET['description'];

$start = $GET_start_date . " " . $GET_start_time;
$end = $GET_start_date . " " . $GET_end_time;



$conn = mysql_connect($DB_HOST, $DB_USER, $DB_PASS)or die ('Error connecting to mysql');
$select = mysql_select_db($DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if($select){
    if ($GET_repeats == 0) {

        $query = mysql_query("INSERT INTO REMINDERS (u_id, title, start_date, start_time, end_time, weekday, repeats, repeats_freq, location, description)
                              VALUES ('$GET_userID', '$GET_title','$GET_start_date'  , '$GET_start_time', '$GET_end_time', '$GET_weekday', '$GET_repeats', '$GET_repeats_freq' ,'$GET_location','$GET_description' )");
        if (!$query) {
            die('Invalid SELECT query: ' . mysql_error());
        }

        $temp = mysql_query("SELECT parent_id FROM REMINDERS ORDER BY parent_id DESC LIMIT 1");
        $array = mysql_fetch_row($temp);
        $parent_id = $array[0];

        $query2 = mysql_query("INSERT INTO REMINDERS_RECURRANCY (parent_id, u_id, title, start, end)
                              VALUES ('$parent_id', '$GET_userID', '$GET_title', '$start', '$end')");

        if (!$query2) {
            die('Invalid SELECT query: ' . mysql_error());
        }
    } // end of repeats
    else {

        $until = (18260/$GET_repeats_freq); // 50 years from now, including leap years
        if ($GET_repeats_freq == 1) {
            $weekday = 0;
            $freq = 1;
        }
        else if ($GET_repeats_freq == 7) {
            $freq = 7;
        }
        else if ($GET_repeats_freq == 365) {
            $freq = 365;
        }

        $query = mysql_query("INSERT INTO REMINDERS (u_id, title, start_date, start_time, end_time, weekday, repeats, repeats_freq, location, description)
                              VALUES ('$GET_userID', '$GET_title','$GET_start_date'  , '$GET_start_time', '$GET_end_time', '$GET_weekday', '$GET_repeats', '$GET_repeats_freq' ,'$GET_location','$GET_description' )");

        if (! $query) {  die('Invalid INSERT query: ' . mysql_error()); }

        $temp = mysql_query("SELECT parent_id FROM REMINDERS ORDER BY parent_id DESC LIMIT 1");
        $array = mysql_fetch_row($temp);
        $parent_id = $array[0];

        for($x = 0; $x < $until; $x++) {
            if ($GET_repeats_freq == 30) {
                $year = date('y', strtotime($start));
                $month = date('m', strtotime($start));

                switch ($month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        $freq = 31;
                        break;
                    case 2:
                        if ($year % 4 == 0) { // leap year
                            $freq = 29;
                        }
                        else {
                            $freq = 28;
                        }
                        break;
                    default:
                        $freq = 30;
                }// end of switch
            } //end of if
            $query2 = mysql_query("INSERT INTO REMINDERS_RECURRANCY
                    (title, start, end, parent_id, u_id)
                    VALUES ('$GET_title', '$start', '$end', '$parent_id', '$GET_userID')");

            $start_date = strtotime($start . '+' . $freq . 'DAYS');
            $end_date = strtotime($end . '+' . $freq . 'DAYS');
            $start = date("Y-m-d H:i", $start_date);
            $end = date("Y-m-d H:i", $end_date);


           if (!$query2) {
                die('Invalid INSERT query: ' . mysql_error());
                print("<script type = text/javascript>
                alert (\"Sorry, something went wrong! The remind has not been created.\");
                window.location.href = \"calendar.php\";</script>");
            }// end of if
        }// end of for
    }
}// end of $select

mysqli_close($conn);